package com.lsl.crm.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.lsl.crm.function.CrmDao;
import com.lsl.crm.vo.User;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
	private CrmDao icd;

	
	public CrmDao getIcd() {
		return icd;
	}


	public void setIcd(CrmDao icd) {
		this.icd = icd;
	}

	public String exit(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String result="exit";
		HttpSession session=request.getSession();
		session.invalidate();
		return result;
	}
	public String login(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session=null;
		String name=request.getParameter("name");
		String password=request.getParameter("password");
		System.out.println("name="+name);
		System.out.println("password="+password);
		session=request.getSession();
		String result="";
		if(session.getAttribute("user")!=null){
			result=SUCCESS;
		}else{
			User user=icd.login(name, password);
			if(user!=null){
				request.setAttribute("message","right");
				session.setAttribute("user",user);
				result=SUCCESS;
			}else{
				request.setAttribute("message","error");
				result=ERROR;
			}
		}
		return result;
	}
}
